Λίστες

Βασικές έννοιες

lst=List[2,3,5]

lst

lst={2,3,5}

lst

{2,(1/2),0.34,a,b->(1/3)}

{{1,2},{3,4}}

??Sin

Sin[{1,2,3}]

Expand[{x+y,(((x+y))^2),(((x+y))^3)}]

5+{1,2,3}

2+3 {1,2,3}

(({1,2,3})^2)

(2^({1,2,3}))

{1,2,3}+{4,5,6}

(({1,2,3})^({1,2,3}))

{1,2}+{2,3,4}

Πίνακες - Η εντολή Table

Table[2 i+1,{i,1,15,2}]

Table[Exp[((2 π I n)/5)],{n,0,4}]

Table[Exp[I x],{x,0,((8 π)/5),((2 π)/5)}]

Table[i,{i,3,11,2}]

Table[RandomReal[],{i,1,10,1}]

RandomReal[{0,1},10]

Table[(n^2),{n,1,20}]

Άλλα παραδείγματα

rand=RandomReal[{0,1},5]

TableForm[rand]

rand//TableForm

rand=RandomReal[{0,1},5]

rand+5

rand=RandomReal[{0,1},5]//TableForm

rand+5

rand=RandomReal[{0,1},5]

rand+5//TableForm

rand=RandomReal[{0,1},100]

Short[rand,10]

Short[rand,3]

rand

Short[rand,12]

Clear[rand]

Table[Prime[i],{i,20}]

Η εντολή TableForm

Table[Table[10 i+j,{j,6}],{i,5}]//TableForm

Table[10 i+j,{i,5},{j,6}]//TableForm

TableForm[%]

TableForm[Table[(i^2)-(j^2),{i,4},{j,7}]]

Πράξεις σε λίστες

Τμήματα

data={1,3,5,7,9};

Part[data,2]

data[[2]]

data

data[[2]]=25;
data

data[[12]]

??Part

?Listable

data[[{1,2,3}]]

data[[{1,2,1,3,1}]]

Clear[data]

Range[10]

Range[1.5, 7.8,0.2]

ints=Table[i,{i,20,30}]

First[ints]

Last[ints]

ints[[Length[ints]]]

ints[[-1]]

Part[ints,-2]

ints[[-1]]

ints[[-3]]

Υπολίστες

ints

Length[ints]

Take[ints,5]

Take[ints,-3]

Take[ints,{3,7}]

Drop[ints,2]

Drop[ints,-4]

Drop[ints,{2,5}]

First[ints]

Rest[ints]

Clear[ints]

Άλλα παραδείγματα

coords={{1,2},{2,3},{3,5},{4,7},{5,11}};

coords//TableForm

coords[[1]]

coords[[1]][[1]]

coords[[1,1]]

coords[[3]][[2]]

coords[[3,2]]

Part[coords,3,2]

Clear[coords]

primeList=Table[Prime[i],{i,100}]

First[primeList]

Last[primeList]

primeList[[33]]

primeList[[{10,20,30}]]

primeList[[15]]=69

primeList

primeList[[15]]=47

Take[primeList,10]

Take[primeList,-20]

Take[primeList,{34,43}]

Take[primeList,50]

Drop[primeList,-50]

Drop[primeList,{15}]

Drop[primeList,{11,90}]

Clear[primeList]

Συνολοθεωριτικές Πράξεις

Παραδείγματα

?Join

Join[{1,3,5,7,9},{2,3,5,7}]

Length[{a,b,c,d}]

Length[{1,{1,2},3}]

?Union

?Intersection

Union[{1,2,1,3,2,4,5,6,4,5,3,7,8}]

Union[{1,3,5,7,9},{2,3,5,7}]

Intersection[{1,3,5,7,9},{2,3,5,7}]

?Complement

Complement[{1,3,5,7,9},{2,3,5,7}]

?Split

Split[{1,2,2,3,3,5,3,4,4,4,4}]

Partition[{1,2,2,3,3,5,3,4,4,4,4}, 4,2]

Complement[Table[i,{i,1,100}],Table[i,{i,2,100,2}],Table[i,{i,3,100,3}]]

data=RandomInteger[{0,1},100]

sr=Split[data]

Map[Length, sr]

Max[%]

Union[sr]

Η εντολές Apply, Map, Select.   Ανώνυμες (pure) συναρτήσεις

FullForm[{1,2,3}]

?Apply

Apply[f,{1,2,3}]

Apply[Plus,{1,2,3}]

Apply[f,g[1,2,3]]

Map[f,{1,2,3}]

Map[Sin,{1,(π/2),2,π}]

f[x_]:=(x^2)

Map[f,{1,(π/2),2,π}]

Apply[f,{1,(π/2),2,π}]

Clear[f]

h=Function[x,x^2-x]

h[4]

?h

h[4,2]

Function[x,x^2-x][3]

Map[Function[x,(x^2)],{1,(π/2),2,π}]

?Function

Function[x,(x^2)][z+y]

Function[x,x[[1]]][{1,2,3,4}]

Map[Function[x,x[[1]]],{{1,2},{3,4},{5,6}}]

f[x_]:=x>=0

f[1]

f[-1]

Clear[f]

g[x_]:=x≥0

{g[1],g[0],g[-1]}

Function[x,x>=0][1]

(#>=0)&

FullForm[(#>=0)&]

(#>=0)&[1]

f:=(#>=0)&

f[1]

(#1>#2)&[10,15]

(#^2-2#+10)&[10]

Clear[f]

Map[f,{a,b,c}]

Map[(#>=0)&,{-2,-1,0,1,2}]

?Select

Select[{-2,-1,0,1,2},(#>=0)&]

num=Table[i,{i,1,50}]

?*Q

Select[num,EvenQ]

Select[num,PrimeQ]

Select[num,(Mod[#,2]==0&&Mod[#,3]!=0)&]

Clear[num]

coords=Table[RandomReal[{-1,1}],{i,1,10},{j,1,2}]

coords=Table[RandomReal[{-1,1}],{i,1,10},{j,1,2}]

TableForm[Map[Function[c,c[[1]]],coords]]

TableForm[Map[Function[x,(Sqrt[((x[[1]])^2)+((x[[2]])^2)])],coords]]

TableForm[Map[Sqrt[#[[1]]^2+#[[2]]^2]&,coords]]

hyp[c_]:=Sqrt[c[[1]]^2+c[[2]]^2]
TableForm[Map[hyp,coords]]

coords

hypot[c:{_Real,_Real}]:=Sqrt[c[[1]]^2+c[[2]]^2]
TableForm[Map[hyp,coords]]

hypot[c:{x_Real,y_Real}]:=Sqrt[x^2+y^2]
TableForm[Map[hyp,coords]]

TableForm[Map[Function[x,{x[[2]],x[[1]]}],coords]]

Clear[coords]

data=Table[RandomInteger[{1,6}],{i,1,100}]

Count[data,6]

Length[Select[data,(#==6)&]]

?Count

dice[n_,k_]:=Module[{data,len,sublst},data=Table[RandomInteger[{1,6}],{i,1,n}];sublst=Select[data,#1==k&];len=Length[sublst];{n,k,len}]

dice[100,5]

diceExper=Table[dice[100,5],{1000}];

Length[diceExper]

N[Apply[Plus,Map[Last,diceExper]]/1000]

Length[diceExper]

diceaverage[n_,k_,m_]:=Module[{diceExper,ave},
diceExper=Table[dice[n,k],{m}];
ave=N[Apply[Plus,Map[Last,diceExper]]/m];
{n,k,m,ave}]

diceaverage[100,6,1000]

Άλλες εντολές

lst=RandomInteger[6,15]

Count[lst,2]

Count[lst, _Real]

Position[lst,4]

lst1={a,{b,{{c},d}},{a,e}}

p=Position[{a,{b,{{c},d}},{a,e}},a]

Part[lst1,Apply[Sequence,{3,1}]]

Map[Part[lst1,Apply[Sequence,#]]&,p]

Position[{a,{b,{{c},d}},{a,e}},c]

MemberQ[lst,1]

Select[lst,EvenQ]

lst

FreeQ[lst,x]

FreeQ[lst, 0]

FreeQ[lst,_Integer]

FreeQ[lst,_Real]

Η εντολή Sort

Sort[{1,π,a,"and",{1,2}}]

?Sort

Sort[{1,2,3,4,π,Sin[1]//N,(3/2)}]

N[{1,2,3,4,π,Sin[1],(3/2)}]

Sort[%]

f=Function[{x,y},3x+(y^2)]

f[x,y]

f=(3 #1+((#2)^2))&

f[x,y]

Clear[f]

Sort[{4,π,3,Sin[1],2,1/2,2,1},(#1<#2)&]

Sort[{4,π,3,Sin[1],2,1/2,2,1},(#1>#2)&]

Sort[{4,π,3,Sin[1],2,1/2,2,1},IntegerQ[#1]&]

Sort[{1,2,3,4,5,6,7,8},EvenQ[#1]&]

Table[RandomReal[{-1,1}],{i,1,10},{j,1,2}]

Sort[%,(((#1[[1]])^2)+((#1[[2]])^2)<((#2[[1]])^2)+((#2[[2]])^2))&]//TableForm

Άλλες  εντολές (Append,Prepend,Insert,Delete,ReplacePart, Partition, RotateLeft,Reverse,Transpose,Flatten)

lst=RandomInteger[{1,10},5]

Dimensions[%]

Table[RandomInteger[{1,10}],{i,5}]

Dimensions[%]

Table[{i,j},{i,5},{j,5}]

Dimensions[%]

Table[RandomInteger[{10,20},2],{i,5}]

Transpose[%]

Flatten[{1,{2},{{3},{4}}}]

Flatten[{1,{2},{{3},{4}}},1]

t=Table[i,{i,15}]

Partition[t,3]

Partition[t,3,2]

lst

Append[lst,100]

lst

AppendTo[lst,100]

lst

Select[Flatten[Table[{i,j,(Sqrt[(i^2)+(j^2)])},{i,1,100},{j,1,100}],1],(IntegerQ[#[[3]]])&]

Length[%]

lst

RotateLeft[lst,2]

RotateRight[lst,2]